package com.epcs.model.dao;

import java.util.ArrayList;
import java.util.List;

import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.epcs.model.dto.Account;
import com.epcs.model.dto.Email;
import com.epcs.model.dto.Event;
import com.epcs.model.dto.Following;
import com.epcs.model.dto.Notification;
import com.epcs.util.DBUtil;
import com.epcs.util.EpcsDateTimeUtil;

public class NotificationDAO {
	public List<Notification> getAllNotification(Following following) {
		Session session = null;
		int status = 0;
		int status1 = 1;
		try {

			// get session
			session = DBUtil.getSession();
			Criteria criteria = session.createCriteria(Notification.class);
			Criterion criterion = Restrictions.eq("following", following);
			Criterion criterion1 = Restrictions.eq("status", status);
			Criterion criterion2 = Restrictions.eq("status", status1);
			LogicalExpression orExp = Restrictions.or(criterion1, criterion2);
			criteria.add(criterion).add(orExp).addOrder(Order.asc("lastUpdated"));	
			List<Notification> notifyList = new ArrayList<Notification>();
			notifyList = criteria.list();
			if (notifyList.size() > 0) {
				return notifyList;
			}

		} catch (HibernateException e) {
			System.out.println("Rolback: \n" + e.getMessage());

		} finally {
			if (session != null) {
				session.close();
			}
		}
		return null;

	}
	public List<Notification> getAllNotification1(List<Following> followingList) {
		Session session = null;
		int status = 0;
		int status1 = 1;
		try {

			// get session
			session = DBUtil.getSession();
			Criteria criteria = session.createCriteria(Notification.class);
			Criterion criterion = Restrictions.in("following", followingList);
			Criterion criterion1 = Restrictions.eq("status", status);
			Criterion criterion2 = Restrictions.eq("status", status1);
			LogicalExpression orExp = Restrictions.or(criterion1, criterion2);
			criteria.add(criterion).add(orExp).addOrder(Order.asc("lastUpdated"));	
			List<Notification> notifyList = new ArrayList<Notification>();
			notifyList = criteria.list();
			if (notifyList.size() > 0) {
				return notifyList;
			}

		} catch (HibernateException e) {
			System.out.println("Rolback: \n" + e.getMessage());

		} finally {
			if (session != null) {
				session.close();
			}
		}
		return null;

	}

	public List<Notification> getUnloadNotification(List<Following> followingList) {
		Session session = null;
		int status = 0;
		try {

			// get session
			session = DBUtil.getSession();
			Criteria criteria = session.createCriteria(Notification.class);
			Criterion criterion = Restrictions.in("following", followingList);
			Criterion criterion1 = Restrictions.eq("status", status);
			criteria.add(criterion).add(criterion1).addOrder(Order.asc("lastUpdated"));
			List<Notification> notifyList = new ArrayList<Notification>();
			notifyList = criteria.list();
			if (notifyList.size() > 0) {
				return notifyList;
			}

		} catch (HibernateException e) {
			System.out.println("Rolback: \n" + e.getMessage());

		} finally {
			if (session != null) {
				session.close();
			}
		}
		return null;

	}

	public Notification getNotification(int notificationID) {

		Session session = DBUtil.getSession();
		try {
			Criteria criteria = session.createCriteria(Notification.class);
			criteria.add(Restrictions.eq("notificationId", notificationID));
			Notification notify = (Notification) criteria.uniqueResult();
			if (notify != null) {
				return notify;
			}
		} finally {

			if (session != null) {
				session.close();
			}
		}

		return null;
	}

	public boolean updateNotification(int notificationID) {

		Notification notification = getNotification(notificationID);
		notification.setStatus(1);
		Session session = DBUtil.getSession();
		Transaction trans = null;
		try {
			trans = session.beginTransaction();
			session.update(notification);
			trans.commit();
			return true;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			trans.rollback();
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return false;
	}

	public boolean insertNotificationToDB(Notification notification) {

		Session session = null;
		Transaction transaction = null;
		try {

			// get session
			session = DBUtil.getSession();
			// open transaction
			transaction = session.beginTransaction();
			session.save(notification);
			transaction.commit();

			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			System.out.println("Rolback: \n" + e.getMessage());

		} finally {
			if (session != null) {
				session.close();
			}
		}
		return false;
	}

	public boolean deleteNotification(Notification notification) {

		Session session = DBUtil.getSession();
		Transaction trans = null;
		try {
			trans = session.beginTransaction();
			session.delete(notification);
			trans.commit();
			return true;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			trans.rollback();
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return false;
	}

	public List<Notification> getNotificationByFollowing(Following following) {
		Session session = null;
		try {
			;
			// get session
			session = DBUtil.getSession();
			Criteria criteria = session.createCriteria(Notification.class);
			Criterion criterion = Restrictions.eq("following", following);

			criteria.add(criterion);
			List<Notification> notifyList = new ArrayList<Notification>();
			notifyList = criteria.list();
			if (notifyList.size() > 0) {
				return notifyList;
			}

		} catch (HibernateException e) {
			System.out.println("Rolback: \n" + e.getMessage());

		} finally {
			if (session != null) {
				session.close();
			}
		}
		return null;

	}
	
	public Notification getNotificationByType(Following following, String type) {
		
		Session session = null;
		try {
			
			// get session
			session = DBUtil.getSession();
			Criteria criteria = session.createCriteria(Notification.class);
			criteria.add(Restrictions.eq("following", following)).add(Restrictions.eq("typeOfNotification",type));
			return (Notification)criteria.uniqueResult();
		} catch (HibernateException e) {
			System.out.println("Rolback: \n" + e.getMessage());
			
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return null;
	}
	public boolean updateNotificationbyID(Notification notification) {
	
		Session session = DBUtil.getSession();
		Transaction trans = null;
		try {
			trans = session.beginTransaction();
			session.update(notification);
			trans.commit();
			return true;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			trans.rollback();
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return false;
	}
	
	public boolean updateNotificationComment(Notification notification) {
		Session session = DBUtil.getSession();
		Transaction trans = null;
		try {
			trans = session.beginTransaction();
			session.update(notification);
			trans.commit();
			return true;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			trans.rollback();
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return false;
	}
	public List<Notification> getAllNotificationForAccount(List<Following> followingList) {
		Session session = null;
		try {

			// get session
			session = DBUtil.getSession();
			Criteria criteria = session.createCriteria(Notification.class);
			Criterion criterion = Restrictions.in("following", followingList);
			criteria.add(criterion).addOrder(Order.asc("status")).addOrder(Order.desc("lastUpdated"));
			List<Notification> notifyList = new ArrayList<Notification>();
			notifyList = criteria.list();
			if (notifyList.size() > 0) {
				return notifyList;
			}

		} catch (HibernateException e) {
			System.out.println("Rolback: \n" + e.getMessage());

		} finally {
			if (session != null) {
				session.close();
			}
		}
		return null;

	}
}
